/*
 * @Author: humengchuan 531537052@qq.com
 * @Date: 2024-03-26 23:34:24
 * @LastEditors: humengchuan 531537052@qq.com
 * @LastEditTime: 2024-06-27 16:59:55
 * @FilePath: \project\node-project\server\controller\admin\UserController.js
 * @Description: 控制层
 */
const UserService = require("../../services/admin/UserService");
const JWT = require("../../util/JWT");

const userController = {
  login: async (req, res) => {
    let result = await UserService.login(req.body);
    if (result.length === 0) {
      return res.send({
        code: -1,
        msg: "用户名或密码错误"
      });
    } else {
      const Token = JWT.generate({
        id: result[0].id
      }, "2h")
      
      res.header("Authorization", Token); // 设置token
      // res.header = { "Access-Control-Expose-Headers": "Authorization" };
      return res.send({
        code: 200,
        msg: "登录成功",
        data: {token: Token, data: result[0]}
      });
    }
  },
  info: async (req, res) => {
    // console.log('%c [ req, res ]-30', req, res)
    // let result = await UserService.info(req.query.id);
    // console.log('1111;', result)
    return res.send({
      code: 200,
      msg: "获取成功",
      data: {
        msg: 'hello nice to meet you!'
      }
    });
  },
  list: async (req, res) => {
    let result = await UserService.list();
    return res.send({
      code: 200,
      msg: "获取成功",
      data: result
    });
  }
};

module.exports = userController;